<?php
	require_once('database.php');
	$project_id = $_POST['id'];
	if ($db = new SQLiteDatabase('cs147organizer.db')) {
		$command = "SELECT * FROM projects WHERE project_id=%d LIMIT 1";
		$replaced = sprintf($command,$project_id);
		$result = $db->query($replaced);
		$proj = array();
		if($result->valid()){
			$cur = $result->current();
			$proj['project_name'] = stripslashes($cur['project_name']);
			$proj['project_id'] = $cur['project_id'];
		}	else	{
			echo(0);
			die();
		}
		$proj['members'] = array();
		$command = "SELECT * FROM project_membership WHERE project_id=%d";
		$replaced = sprintf($command,$project_id);
		$result = $db->query($replaced);
		while($result->valid())	{
			$cur = $result->current();
			$query = "SELECT * FROM users WHERE user_id=%d LIMIT 1";
			$query = sprintf($query,$cur['user_id']);
			$r = $db->query($query);
			if($r->valid())	{
				$c = $r->current();
				array_push($proj['members'], $c['username']);
			}
			$result->next();
		}
		
		$proj['contacts'] = '';
		$command = "SELECT * FROM users
		WHERE deleted=0";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			if(checkIsUserInProject($cur['user_id'], $project_id))	{
				$result->next();
				continue;
			}
			$proj['contacts'] .= "<li><input type=\"checkbox\" id=\"pshare" . $cur['user_id'] . "\" class=\"invitee\" title=\"".$cur['username']."\" /></li>\n";
			$result->next();
		}
		echo(json_encode($proj));
	} else {
		die($err);
	}
?>